package com.yx.frame.jdbc.pool;

public class JdbcConfig {
	private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	String id = null;

	boolean defaultJdbc = false;
	boolean oracleOrMysql = true;
	String host = null;
	int port = -1;
	String dbName = null;
	String userName = null;
	String password = null;
	int minConnectionSize = 5;
	int maxConnectionSize = 20;
	int maxQueryRowCount = 5000;
	int getConnectionTimeout = 10;
	int maxIdleTime = 240;
	int acquireIncrement = 5;

	public JdbcConfig(String id, boolean defaultJdbc, boolean oracleOrMysql, String host, int port, String dbName,
			String userName, String password, int minConnectionSize, int maxConnectionSize) {
		this.id=id;
		this.defaultJdbc=defaultJdbc;
		this.oracleOrMysql=oracleOrMysql;
		this.host=host;
		this.port=port;
		this.dbName=dbName;
		this.userName=userName;
		this.password=password;
		this.minConnectionSize=minConnectionSize;
		this.maxConnectionSize=maxConnectionSize;
	}
	
	public JdbcConfig(String id, boolean defaultJdbc, boolean oracleOrMysql, String host, int port, String dbName,
			String userName, String password, int minConnectionSize, int maxConnectionSize,int maxQueryRowCount) {
		this.id=id;
		this.defaultJdbc=defaultJdbc;
		this.oracleOrMysql=oracleOrMysql;
		this.host=host;
		this.port=port;
		this.dbName=dbName;
		this.userName=userName;
		this.password=password;
		this.minConnectionSize=minConnectionSize;
		this.maxConnectionSize=maxConnectionSize;
		this.maxQueryRowCount=maxQueryRowCount;
	}
	
	//id:defaultJdbc:oracleOrMysql:host:port:dbName:userName:password:minConnectionSize:maxConnectionSize
	public JdbcConfig(String jdbcStr)throws Exception{
		String[] jdbc=jdbcStr.split("\\:");
		this.id=jdbc[0];
		if("Y".equals(jdbc[1])){
			this.defaultJdbc=true;
		}else{
			this.defaultJdbc=false;
		}
		if("Y".equals(jdbc[2])){
			this.oracleOrMysql=true;
		}else{
			this.oracleOrMysql=false;
		}
		this.host=jdbc[3];
		this.port=Integer.parseInt(jdbc[4]);
		this.dbName=jdbc[5];
		this.userName=jdbc[6];
		this.password=jdbc[7];
		this.minConnectionSize=Integer.parseInt(jdbc[8]);
		this.maxConnectionSize=Integer.parseInt(jdbc[9]);
	}

	public String getId() {
		return id;
	}

	public String getUrl() {
		if (oracleOrMysql) {
			return "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbName;
		} else {
			return "jdbc:mysql://" + host + ":" + port + "/" + dbName+"?useUnicode=true&characterEncoding=UTF-8";
		}
	}

	public String getDriver() {
		if (oracleOrMysql) {
			return ORACLE_DRIVER;
		} else {
			return MYSQL_DRIVER;
		}
	}

	public boolean isDefaultJdbc() {
		return defaultJdbc;
	}

	public boolean isOracleOrMysql() {
		return oracleOrMysql;
	}

	public String getDbName() {
		return dbName;
	}

	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}

	public int getMinConnectionSize() {
		return minConnectionSize;
	}

	public int getMaxConnectionSize() {
		return maxConnectionSize;
	}

	public int getMaxQueryRowCount() {
		return maxQueryRowCount;
	}

	public int getGetConnectionTimeout() {
		return getConnectionTimeout;
	}

	public int getMaxIdleTime() {
		return maxIdleTime;
	}

	public int getAcquireIncrement() {
		return acquireIncrement;
	}

}
